in reply to Re^3: Reading a .txt file under 2 levels of compression
in thread Reading a .txt file under 2 levels of compression

Thanks bart for code. It is working! I do have another question regarding the following script: unzip $file, \$inner, Name => '1.zip'; What does \$inner mean? Unzipping file into a temp location called inner?
  • Comment on Re^4: Reading a .txt file under 2 levels of compression

Replies are listed 'Best First'.
Re^5: Reading a .txt file under 2 levels of compression
by pmqs (Friar) on Jan 13, 2011 at 10:56 UTC
    When I wrote unzip I wanted to allow flexibility on where it obtained it's input and where it sent its output. Currently the input and output paramers can be a filename, filehandle or in-memory.

    So, for example

    my $inner; unzip "my.zip" => \$inner; # remove all whitespace $inner =~ s/\s+//g; print $inner;
    This will uncompress the contents of the file "my.zip" and write the uncompressed data into the variable $inner.

    The code then makes use of the uncompressed data in this case to remove all whitespace, then print it.

Re^5: Reading a .txt file under 2 levels of compression
by bart (Canon) on Jan 13, 2011 at 12:17 UTC
    \$inner is a reference to the scalar $inner. It allows programmers to change the content of the variable $inner without explicitly mentioning it by name in the source. You can just as well use a different variable and it'll work the same. For example:
    $apples = 10; $pears = 6; take_one(\$apples); # take one apple take_one(\$pears); # take one pear print "I've still got $apples apples and $pears pears.\n"; sub take_one { my $ref = shift; # a reference $$ref-- # access the value of the referenced scalar }
    This prints:
    I've still got 9 apples and 5 pears.

    For more info, check out perlreftut and perlref.

    It's easy for a programmer to detect if a value is a reference: just use ref: it'll return an empty string for a normal string, and "SCALAR" for a reference to a scalar.

    It's a convention among module authors to use a reference to a scalar if you want to actually use the string value as data, instead of as a file name from which you'll read the contents; for example in the various HTML parsing modules.

      I see. Usually for Methods I would simply read what it requires and what outputs it will give. In the case of Zip, I couldnt find any sources that says the code is valid (like unzip $file, \$inner, Name => '1.zip'; , it still looks wrong to me but it works). Thanks once again.