Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Do not understand code

by BlackKnait (Novice)
on Sep 28, 2017 at 09:55 UTC ( [id://1200253]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all, I found a code snippet within a script and do not know, what it means. I know some basics of perl but I am not really familar with this. Nevertheless I would like to know what this will mean:
unlink $file if /.BGV/;
I know that I will delete the file, but what does this 'if' means? What condition is it? CU

Replies are listed 'Best First'.
Re: Do not understand code
by haukex (Archbishop) on Sep 28, 2017 at 10:03 UTC
    unlink $file if /.BGV/; ... what does this 'if' means? What condition is it?

    The if after a statement is a statement modifier, it is the equivalent of if (/.BGV/) { unlink $file }. The condition is equivalent to $_ =~ /.BGV/, that is, match the special variable $_ against the regex /.BGV/. The special variable $_ gets set in several places, one very common one is the while (<$filehandle>) { ... } loop, see I/O Operators. That line of code in English: If the string stored in $_ contains the four-character sequence of any character (except newline) followed by the string 'BGV', then unlink (delete) the file whose name is given in the string $file.

      Thanks for this detailed answer. I knew its possible to ask for a condition like $_ ~= <regex> - but this notation is new for me. With this information I understand this snippet.

        Note that you reversed the operator. Itís not ~=, itís =~.

Re: Do not understand code
by LanX (Saint) on Sep 28, 2017 at 10:05 UTC
    > What condition is it?

    It is a regex testing the content of $_

    And it looks broken, probably someone thought to only match files which ends on .BGV but it's matching any substring with 4 characters where the last 3 are BGV

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Ah, yes, I understand... :-) Thats quite easy. Thanks a lot for this answer...
Re: Do not understand code
by 1nickt (Canon) on Sep 28, 2017 at 10:06 UTC

    Hi, this is shorthand for

    unlink $file if $_ =~ m/.BGV/;

    The way forward always starts with a minimal test.
      To see it in a long term, its more easy to understand the code. Thanks for this info.
Re: Do not understand code
by ForgotPasswordAgain (Priest) on Sep 29, 2017 at 15:04 UTC
    I don't see it mentioned, so thought I'd point out that /.BGV/ might be buggy for two reasons:
    1. Since . matches any character, /.BGV/ would match for example "foo-BGV".
    2. There's no anchor at the end, so it would match anywhere in the filename, for example "foo-BGV.txt".
    It's possible that that's what was intended, but it likely should be /\.BGV\z/ if it's meant to be a file suffix, and maybe even /\.bgv\z/i if it isn't case-sensitive.
A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1200253]
Approved by Ratazong
Front-paged by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-23 02:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found