Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Iterating over *any* thing in Perl

by pg (Canon)
on Dec 03, 2002 at 06:25 UTC ( #217140=note: print w/replies, xml ) Need Help??


in reply to Iterating over *any* thing in Perl

Object is nothing more than a blessed data structure, i.e. a hash or an arrary etc. (A hash of hash of hash is still a hash at each particular level, so we are dealing with no more than scalar, array/list and hash)

Basically, in order to be able to iterate over a data structure, you only need two things to support you:
  1. A way to determine what the data structure is, and this is not a big deal, as we have the isa function/method. Not just to recognize hash, array etc., you can actually even determine whether it is regexp, code, REF...

    Data Dumper is one of those kind of tools that can iterate over any given data structure. But it only does some pre-determined 'hard-coded' thing for you, for example, to dump the content of the data structure.

  2. A general purpose 'iterator' should allow the user to define call-back functions, so its functionality can be extended by the user. For example, those HTML, or XML parsers support call-back functions.

Replies are listed 'Best First'.
Re: Re: Iterating over *any* thing in Perl
by PodMaster (Abbot) on Dec 03, 2002 at 08:53 UTC
    Just ignore, I don't play the semantics game well (you're right)
    use anal::retentive;
    Except that it's not. An object is nothing more than a blessed reference, not data structure. Observe:
    my %foo = (1,2,3,4); bless %boo,'bar'; __END__ Can't bless non-reference value at - line 2.
    Now you know I like to quote ;) from perldoc -f bless
    bless REF,CLASSNAME
    bless REF
            This function tells the thingy referenced by REF that it is now
            an object in the CLASSNAME package. If CLASSNAME is omitted, the
            current package is used. Because a "bless" is often the last
            thing in a constructor, it returns the reference for
            convenience. Always use the two-argument version if the function
            doing the blessing might be inherited by a derived class. See
            the perltoot manpage and the perlobj manpage for more about the
            blessing (and blessings) of objects.
    
            Consider always blessing objects in CLASSNAMEs that are mixed
            case. Namespaces with all lowercase names are considered
            reserved for Perl pragmata. Builtin types have all uppercase
            names, so to prevent confusion, you may wish to avoid such
            package names as well. Make sure that CLASSNAME is a true value.
    
            See the section on "Perl Modules" in the perlmod manpage.
    
    update: hmm, it's a weird thing, the datastructure is "blessed", but only the reference knows, weird


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2021-05-12 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (125 votes). Check out past polls.

    Notices?