Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Arrow Operator and questions related to Path::Class

by kamal (Sexton)
on Sep 11, 2012 at 13:53 UTC ( #992999=perlquestion: print w/replies, xml ) Need Help??
kamal has asked for the wisdom of the Perl Monks concerning the following question:

I have just started to delve into Perl, and going through the following example:
#!/usr/bin/perl use strict; use warnings; use Path::Class; my $dir = dir('foo','bar'); # foo/bar # Iterate over the content of foo/bar while (my $file = $dir->next) { # See if it is a directory and skip next if $file->is_dir(); # Print out the file name and path print $file->stringify . "\n"; }
Reading the code....
my $dir = dir ('test1');

# local scalar dir is assigned by dir ('test1')
the dir on the RHS, being possibly from Path::Class (CPAN)
while (This condition hold true)
# Simple While

my $file = $dir->next $dir scalar de-references the function next ? HOW ?
HOW do i guess that next would indeed be written as next and not next(), etc
since there is NO reference/example for "next" in Same goes for stringify, or is_dir()
Now in this case is_dir() seems again to be a function, but how come its called differently


Replies are listed 'Best First'.
Re: Arrow Operator and questions related to Path::Class
by choroba (Chancellor) on Sep 11, 2012 at 14:44 UTC
    There are two instances of next in your code. They are very different:
    $dir->next calls the method next of the object $dir, while next if ... jumps to the end of the current loop and iterates a next cycle.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Arrow Operator and questions related to Path::Class
by kennethk (Abbot) on Sep 11, 2012 at 15:22 UTC
    Let's go line by through the code.

    use Path::Class;

    So, we'll use Path::Class. According to the linked docs, this use statement exports 2 functions: dir and file. So now we know anytime we see the function dir, we are invoking the one from Path::Class. From the docs:


    A synonym for Path::Class::Dir->new.

    So the correct place to find docs on the object returned by dir is Path::Class::Dir. Let us continue:
    # Iterate over the content of foo/bar while (my $file = $dir->next) {
    Now we've invoked the method next via an arrow dereference, so we check the appropriate package for the object for the method documentation. If you didn't realize it what package it was, you can use ref to tell you via the debug statement print ref $dir;.
    $dir_or_file = $dir->next()

    A convenient way to iterate through directory contents. The first time next() is called, it will open() the directory and read the first item from it, returning the result as a Path::Class::Dir or Path::Class::File object (depending, of course, on its actual type). Each subsequent call to next() will simply iterate over the directory's contents, until there are no more items in the directory, and then the undefined value is returned....

    So now we have either a Path::Class::Dir or a Path::Class::File, depending on what the iterator hit. In both cases, methods by the name of stringify and is_dir are available and documented in the appropriate docs. When you get to the line

    next if $file->is_dir();

    we see next is not associated with a object dereference, so unless Path::Class exports something by that name, it will be a Perl CORE function. So, if we check the docs, either via the web at next or on the command line via perldoc -f next, we see it's a language keyword for flow control.

    This is all recoverable if you follow the logical flow of the program.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Arrow Operator and questions related to Path::Class
by ww (Archbishop) on Sep 11, 2012 at 14:03 UTC
    "HOW do i guess that next would indeed be written as next and not next()...?"
    perldoc -f next at your command prompt.
    next is an integral flow control function in perl... it's not something special to any particular (non-core) module.
      Thanks, and what about $file->stringify ?
      I got:
      No documentation for perl function `stringify' found
      $perldoc -f stringify

        You will need to learn about the arrow operator ->, for example in perlop, and/or one of the Perl object tutorials, or maybe the Modern Perl book.

        The methods of an object are traditionally documented in the respective class documentation. In your case, that's Path::Class.

Re: Arrow Operator and questions related to Path::Class
by Anonymous Monk on Sep 11, 2012 at 15:00 UTC
    It is actually fairly common in programming-languages for an identifier (such as "next" or "dir") to have multiple meanings depending on the context in which they are used. Furthermore, the Perl language (again, in a manner typical of its genre) defers many aspects of the name-resolution process to very late, at runtime.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992999]
Approved by Corion
[hippo]: The more I look at this code, the more $x is a plain old scalar and the more this condition will never be true. I'm calling it a bug at this point.
[hippo]: Thanks for your input which has soothed my sanity (a little)
[Corion]: Eily: Sure - if you force both things into stringy things, then you break that magic. But that would also mean that you changed the expression, as now $x = 0.00 will be true instead of false as it were before
[Corion]: Ah no, at least in my feeble experiments that doesn't change the meaning
[Corion]: We sell sanity in small packages ;)
[Eily]: oh, boolean context uses the "" overload if no bool overload has been defined
[Eily]: (I wondered if overloading bool was actually necessary)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2017-07-27 13:46 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (414 votes). Check out past polls.