Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How do I determine if a variable contains a type glob?

by BrowserUk (Patriarch)
on Dec 04, 2016 at 21:09 UTC ( [id://1177170]=note: print w/replies, xml ) Need Help??


in reply to How do I determine if a variable contains a type glob?

Does this help?

[0]{} Perl> print ref() for \my $fh, \*fh, my $ref = \*fh;; SCALAR GLOB GLOB

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: How do I determine if a variable contains a type glob?
by ikegami (Patriarch) on Dec 05, 2016 at 01:10 UTC

    No. The OP asked about globs (e.g. *ARGV)

    Furthermore, use of ref instead of reftype will fail if the referenced var is blessed.

      That, and if someone tricks you there

      $ perl -wE'my $foo = bless [],"GLOB"; say ref $_ for $foo, \*ARGV' GLOB GLOB $ perl -MScalar::Util=reftype -wE'my $foo = bless [],"GLOB"; say refty +pe $_ for $foo, \*ARGV' ARRAY GLOB

      Enjoy, Have FUN! H.Merijn

      Man, you really talk a lot of twaddle!

      The OP asked about globs (e.g. *ARGV)

      And what do you think *fh is. (It's rhetorical.)

      Furthermore, use of ref instead of reftype will fail if the referenced var is blessed.

      How can "the reference be blessed", when (as you unnecessarily pointed out yourself) he asked about *GLOB not \*GLOB. It can't; because my suggestion was that he take the reference himself.

      Ie. His sub becomes something like:

      sub isIt{ local $^W; ref( \$_[0] ) =~ m[GLOB] ? 1 : 0; }

      And it is used like this:

      [0]{} Perl> printf "%s: %d\n", $_, isIt( $_ ) for *ARGV, *INC, *SIG, * +MATCH, *FH, *A, *B, *STDOUT, *CORE::say;; # *anything *main::ARGV: 1 *main::INC: 1 *main::SIG: 1 *main::MATCH: 1 *main::FH: 1 *main::A: 1 *main::B: 1 *main::STDOUT: 1 *CORE::say: 1

      And

      ... if the referenced var is blessed.

      You don't (*CAN'T*) bless a var; only a reference to one!

      You can pop-down now.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Just FYI, when you bless a reference, the blessing is actually stored in the referenced variable. So, due to this current implementation detail, your code can actually be tripped up by such a weird scenario:

        my $glob = *X; my $ref = bless \$glob; print ref(\$glob); __END__ main

        - tye        

        And what do you think *fh is.

        A glob, what you should have used instead of \*fh.

        You don't (*CAN'T*) bless a var; only a reference to one!

        Actually, you have that backwards. I've never seen a blessed reference. You have to pass a reference to bless, but it's the var that gets blessed. Otherwise, the following would fail:

        sub foo { CORE::say "ok" } my %h; my $ref1 = \%h; my $ref2 = \%h; bless($ref1); $ref2->foo();

        How can "the reference be blessed",

        I said ref would fail if the glob is blessed.

        $ perl -MScalar::Util=reftype -e' CORE::say ref(\*FOO); CORE::say reftype(\*FOO); bless(\*FOO); CORE::say ref(\*FOO); CORE::say reftype(\*FOO); ' GLOB GLOB main GLOB

        I like the way you disable all warnings. :-) But, why? (I have some ideas, but I ask just to be sure.)

        local $^W;

        Why are you using a regular expression match, rather than string equality 'eq'?

        ref( \$_[0] ) =~ m[GLOB]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-03-28 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found