majo has asked for the wisdom of the Perl Monks concerning the following question:

Can someone please explain what this section of the codes means especially for %h, my $file = 'file'.$pixel.'.jpg' and unlink $file or die $! if -e $file. Thanks
my %h = ( '500x500' => 505544, '750x750' => 1118012, '1000x1000' => 1986284, '1500x1500' => 4468241, '2000x2000' => 7907740, ); my $pixel = '1000x1000'; my $url_file = 'http://speedserver/file'.$pixel.'.jpg'; my $file = 'file'.$pixel.'.jpg'; unlink $file or die $! if -e $file;

Replies are listed 'Best First'.
Re: QUERY ABOUT A SECTION OF A CODE
by aaron_baugher (Curate) on May 16, 2012 at 20:50 UTC

    The first statement creates a hash variable and assigns a group of keys and values to it. The second statement assigns a value to the scalar variable $pixel. The third statement assigns a value to the scalar variable $url_file, concatenating two string values and the value of $pixel. The fourth statement assigns a value to the scalar variable $file, again concatenating two bits of text with the value of $pixel.

    The fifth statement is the most complicated one. It says, "if the file named in $file exists, unlink (delete) the file or exit the program and tell me the most recent error." Using or in this way is an idiomatic way in Perl to say, "do this, and if it fails, do that." So if the unlink succeeds, the or is satisfied and the die is not executed. To rewrite it longhand to make the order of things clearer:

    if( -e $file ){ unless( unlink $file ){ die $!; } }

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re: QUERY ABOUT A SECTION OF A CODE
by snape (Pilgrim) on May 16, 2012 at 20:39 UTC

    %h is hash table. $file is a scalar variable and contains file name and unlink $file, deletes the file.

    my $file = 'file'.$pixel.'.jpg';

    It means that $file = file1000x1000.jpg (is a string type). It concatenates the strings i.e. file, value in $pixel and .jpg as one string. Concatenation

    You need to read about perl first and then ask questions: perldoc, learn Perl

Re: QUERY ABOUT A SECTION OF A CODE
by choroba (Cardinal) on May 17, 2012 at 10:16 UTC
    Crossposted at Stackoverflow. It is considered polite to inform about crossposting so people not visiting both sites do not waste their time in solving a problem already solved at the other site.
      I am sorry about that.