Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Tying $@ - weird behavior

by !1 (Hermit)
on Sep 27, 2004 at 23:38 UTC ( #394406=note: print w/replies, xml ) Need Help??


in reply to Tying $@ - weird behavior

To further this oddity:

package Exception; sub TIESCALAR { bless {}, $_[0] } sub STORE { print "Changing the value from '$@' to '$_[1]' (not really +)\n" } sub FETCH { print "$@\n";42 } package main; my $t = tie $@, 'Exception' or die "Unable to do what you want\n"; my ($foo, $bar) = (0, 0); $@ = "hi"; eval { $foo = 42 / $bar }; print "The value is : $@\n"; $t->STORE("taking a look"); print "Value is still: $@, though\n"; $@ = 'blah'; print "Now the value is : $@\n"; __END__ Changing the value from 'hi' to 'hi' (not really) The value is : Illegal division by zero at - line 10. Changing the value from 'Illegal division by zero at - line 10. ' to 'taking a look' (not really) Value is still: Illegal division by zero at - line 10. , though Changing the value from 'blah' to 'blah' (not really) blah Now the value is : 42

Strange that it is actually manipulating $@ in some fashion when you assign something to it.

Update: Ah, antirice showed me some neat tricks. The problem seems to be that the eval's capture is putting the POK flag (has valid public pointer value) on $@. So when you check $@, perl sees the POK flag and just prints what's contained in the PV. It also seems that the PV gets updated no matter what and that checking $@ in the STORE and FETCH are just getting the PV that was set. Funny thing is that the POK flag gets turned off again when you assign to $@ (guess it realizes it's magical once again?). Code and results follow:

# perl package Exception; sub TIESCALAR { bless {}, $_[0] } sub STORE { } sub FETCH { } package main; use Devel::Peek; my $t = tie $@, 'Exception' or die "Unable to do what you want\n"; my ($foo, $bar) = (0, 0); Dump($@); eval { $foo = 42 / $bar }; Dump($@); $@ = 'blah'; Dump($@); __END__ SV = PVMG(0x8135944) at 0x811b12c REFCNT = 1 FLAGS = (GMG,SMG,RMG,pPOK) IV = 0 NV = 0 PV = 0x8124100 ""\0 CUR = 0 LEN = 240 MAGIC = 0x8127e40 MG_VIRTUAL = &PL_vtbl_packelem MG_TYPE = PERL_MAGIC_tiedscalar(q) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x81269a4 SV = RV(0x8140418) at 0x81269a4 REFCNT = 1 FLAGS = (ROK) RV = 0x811b240 SV = PVHV(0x812670c) at 0x811b240 REFCNT = 2 FLAGS = (OBJECT,SHAREKEYS) IV = 0 NV = 0 STASH = 0x81269ec "Exception" ARRAY = 0x0 KEYS = 0 FILL = 0 MAX = 7 RITER = -1 EITER = 0x0 SV = PVMG(0x8135944) at 0x811b12c REFCNT = 1 FLAGS = (GMG,SMG,RMG,POK,pPOK) IV = 0 NV = 0 PV = 0x8124100 "Illegal division by zero at - line 13.\12"\0 CUR = 39 LEN = 240 MAGIC = 0x8127e40 MG_VIRTUAL = &PL_vtbl_packelem MG_TYPE = PERL_MAGIC_tiedscalar(q) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x81269a4 SV = RV(0x8140418) at 0x81269a4 REFCNT = 1 FLAGS = (ROK) RV = 0x811b240 SV = PVHV(0x812670c) at 0x811b240 REFCNT = 2 FLAGS = (OBJECT,SHAREKEYS) IV = 0 NV = 0 STASH = 0x81269ec "Exception" ARRAY = 0x8127e60 KEYS = 0 FILL = 0 MAX = 7 RITER = -1 EITER = 0x0 SV = PVMG(0x8135944) at 0x811b12c REFCNT = 1 FLAGS = (GMG,SMG,RMG,pPOK) IV = 0 NV = 0 PV = 0x8124100 "blah"\0 CUR = 4 LEN = 240 MAGIC = 0x8127e40 MG_VIRTUAL = &PL_vtbl_packelem MG_TYPE = PERL_MAGIC_tiedscalar(q) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x81269a4 SV = RV(0x8140418) at 0x81269a4 REFCNT = 1 FLAGS = (ROK) RV = 0x811b240 SV = PVHV(0x812670c) at 0x811b240 REFCNT = 2 FLAGS = (OBJECT,SHAREKEYS) IV = 0 NV = 0 STASH = 0x81269ec "Exception" ARRAY = 0x8127e60 KEYS = 0 FILL = 0 MAX = 7 RITER = -1 EITER = 0x0

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2021-09-28 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?