On Jun 13, 2013, at 10:51 AM, lee wrote:

> #!/bin/perl
> 
> use strict;
> use warnings;
> 
> chomp $ARGV[0];
> my @info = (stat ($ARGV[0] )  );
> 
> printf("%s: %d bytes, %d mtime\n", $ARGV[0], $info[7], $info[9] );
> 
> 
> Some more questions to this:
> 
> 
> + Is "chomp $ARGV[0];" even allowed?  I wouldn't do anything like that
>  in C :)

chomp($ARGV[0]) does nothing, because the elements of the @ARGV array do not 
have end-of-line characters (not even the last one). chomp is used after 
reading a line from a file with the read line operator <>, which leaves the EOL 
character at the end.

> 
> + Is "print" and "printf" pretty much the same thing implementation
>  wise?  I'm wondering if "printf" might involve more overhead so it
>  might be less efficient, depending on what you're doing.

They are pretty much the same. print will use its default formats for 
converting numerical data to strings. printf will use the format specifiers you 
provide. There can't be much difference in execution speed, probably not even 
enough to measure.

> 
> + When I create files with lines like "filename:size:mtime" or
>  "filename:hash", is there something built in to read a syntax like
>  this into, let's say, an array like "my @fileinfo;" with $fileinfo[0]
>  being the file name, $fileinfo[1] the hash?
>  Such 'key:value:...' combinations is probably something frequently
>  used.  I'm finding some examples to this and don't know what would be
>  the best way to do it.


The split function is the normal way of parsing lines like 'filename:hash'. You 
have to make sure that your data fields do not contain the ':' character, or 
whatever character you use to delimit your fields.

  my @fields = split(':',$line);

For more complicated formats, the Text::CSV module can be used.

You could also use the Storable or Data::Dumper modules to write out a hash or 
array containing your data structure to a file and read it back the next time 
you run your program.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to