Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: First Question, why something works? (-M)

by toolic (Bishop)
on Apr 16, 2014 at 12:30 UTC ( [id://1082496]=note: print w/replies, xml ) Need Help??


in reply to First Question, why something works? (-M)

-M uses $_. Tip #6 from the Basic debugging checklist ... B::Deparse

sub BEGIN { use warnings; use strict 'refs'; require 5.01; } use warnings; use strict 'refs'; BEGIN { $^H{'feature_say'} = q(1); $^H{'feature_state'} = q(1); $^H{'feature_switch'} = q(1); } die "No file names supplied!\n" unless @ARGV; my $oldest_name = shift @ARGV; my $oldest_age = -M $oldest_name; foreach $_ (@ARGV) { my $age = -M $_; ($oldest_name, $oldest_age) = ($_, $age) if $age > $oldest_age; } printf "The oldest file was %s, and it was %.1f days old\n", $oldest_n +ame, $oldest_age;

See also How do I compose an effective node title?

Replies are listed 'Best First'.
Re^2: First Question, why something works? (-M)
by i255d (Initiate) on Apr 16, 2014 at 12:44 UTC
    So what you are saying is that when I use $age = -M; in my foreach loop, it is the same as me using $age = -M $_;?

      Hello i255d, and welcome to the Monastery!

      Yes, this is one of many places where Perl uses the special $_ variable implicitly, making it easier to write concise, elegant code. For a list of the places where Perl uses $_ implicitly, see the first entry in perlvar#General-Variables. (If you’re not already familiar with the Perl documentation site, bookmark it now and get to know your way around as soon as possible.)

      The Camel Book (4th Edition, p. 706) has this advice:

      Use the singular pronoun to increase readability:

      for (@lines) { $_ .= "\n"; }

      The $_ variable is Perl’s version of a pronoun, and it essentially means “it”. So the code above means “for each line, append a newline to it.”

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Very excited to be part of the Monastery. I am still on the llama book, chapter 13. Looking forward to the next one. I am an administrator, so I felt it best not to rush through the file and directory sections of this book. Yes, I think you have answered my questions. I see someone ran my code through B::Deparse, later I will have to get into what that is.
      Yes. If the argument is omitted, file test operators test $_ (except -t which tests STDIN).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1082496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found