Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: detecting an undefined variable

by LanX (Saint)
on Sep 21, 2019 at 16:28 UTC ( [id://11106501]=note: print w/replies, xml ) Need Help??


in reply to Re: detecting an undefined variable
in thread detecting an undefined variable

I mostly agree, but

> Exists refers to keys in a hash and has nothing to do with variables.

this statement is problematic because Perl's namespaces - including scratchpads° - are actually implemented as hashes.

for instance it's perfectly possible to check the STASH for a package variable

DB<3> p exists $main::{xxx} DB<4> $xxx = 1 DB<5> p exists $main::{xxx} 1

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

°) though you'd need PadWalker to access it.

Replies are listed 'Best First'.
Re^3: detecting an undefined variable
by Haarg (Priest) on Sep 21, 2019 at 20:45 UTC

    That doesn't check if the variable exists though, it checks if the stash for that name is populated with anything. Having a sub or file handle with the same name would cause the stash entry to exist, usually stored inside a GLOB. While the other types can be detected, it isn't possible to distinguish between an undefined and non-existent scalar by inspecting a GLOB in pure perl.

      You are right, exists doesn't work reliably on glob-slots. :/

      I should have known better, already got bitten by it in the past.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^3: detecting an undefined variable
by LloydRice (Beadle) on Sep 21, 2019 at 16:50 UTC
    Thanks for all the replies to my "detecting an undefined ..". I agree with the last reply, that I meant "declared" rather than "defined". It's not clear to me why what I'm trying to do should be so undisciplined (ill advised?). I often put together pieces of code from various places. So it's handy to check whether some variable already exists in another piece. In the example I gave, the code would be in a function being called from some other code, written at a different time. If I had then used a variable called $scale, it would be handy to know whether that variable existed in the calling code or whether I now had to declare and define it anew. If it existed before, I would want to use the existing value. If not, I would define it as
    my $scale = 1;
    That doesn't seem so undisciplined to me.
      > That doesn't seem so undisciplined to me.

      It's fragile and opens the door to many hard to debug potential problems.

      Though most people were interpreting your intention as trying to use $$symbolic dereference. That's why I asked for more context.

      Using hashes for optional values is a common pattern.

      > I often put together pieces of code from various places.

      Well, the common pattern for that class of problems is to use modules with stable APIs.

      If you really want to do code generation by composing code chunks, then consider using package declarations and inspecting an our $scale variable in the STASH

      use strict; use warnings; package Chunk; #our $scale =42; package main; my $myscale = $Chunk::{scale} // 1; print $myscale;

      but now that you use packages you're only one step away of using modules ...

      HTH! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Rolf: I appreciate the detailed response. I have tried to use both packages and modules from time to time, but I cannot remember the details of how to do things with either, so I tend to stick to what I can remember. I do use both 'strict' and 'my', but have not figured out how to use 'our' effectively. Actually, I described the history backwards of the calling and the called. Typically, the function in question would be written long before the calling code and would, in fact, end up being called by many different bits of code. Some might define $scale one way, some another, and some not at all. I like the eval() solution and will go with that. Thanks to all.

Log In?
Username:
Password:

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

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

    No recent polls found