-2
#!/usr/bin/perl -w
use strict;

my $args;
$args=@ARGV;

if ($args<1)
{
    die("Expecting a filename");
}
sub showstat
{

    print " $_[0] dev, device number of filesystem\n";
      print " $_[1] ino,      inode number\n";
      print " $_[2] mode,     file mode  (type and permissions)\n";
      print " $_[3] nlink,    number of (hard) links to the file\n";
}

my $filename=$ARGV[0];
if (open (TESTFILE,$filename))
{
    my @filedata= stat(TESTFILE);

    &showstat(@filedata);
    close (TESTFILE);
}
else
{
    print "error on open $filename";

}

My question is if we set a variable ($args) for the array (@ARGV), when we print the line under the sub showstat, shouldn't it be print"$args[0] ..." print "$args[1]"? And for $filename=$ARGV[0], shouldn't it be $filename=$args[1]?

And under if statement condition, where it says &showstat(filedata), what does & mean? when I've looked it up, it meant "part of", but I am confused with this line that is written.

closed as off topic by Anthon, jasonwryan, slm, manatwork, Ulrich Dangel Jun 5 '13 at 6:56

Questions on Unix & Linux Stack Exchange are expected to relate to Unix or Linux within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

1

my question is if we set a variable ($args) for the array (@ARGV), when we print the line under the sub showstat, shouldn't it be print"$args[0] ..." print "$args[1]"?

It shouldn't and must not be $args[0] because the parameters passed to subroutine in Perl are stored in @_ array. If you want to get value of 1st parameter, you must use $_[0]

and for $filename=$ARGV[0], shouldn't it be $filename=$args[1]?

What is $filename? If it is name of script, you must use $0 instead of $ARGV[0]. $ARGV[0] is the first parameter passed to the script.

and under if statement condition, where it says &showstat(filedata), what does & mean?

It just means "Call subroutine showstat".

  • then why did we set this up? $args; $args=@ARGV; – luc Jun 5 '13 at 4:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.