[25247] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 7492 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Dec 7 21:05:51 2004

Date: Tue, 7 Dec 2004 18:05:10 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 7 Dec 2004     Volume: 10 Number: 7492

Today's topics:
        better way of Event.pm usage <pobugfix@peterlink.ru>
        Determining connection type <mdudley@king-cart.com>
    Re: Determining connection type <spamtrap@dot-app.org>
    Re: Determining connection type (Michael Fuhr)
    Re: Determining connection type <mdudley@king-cart.com>
    Re: Determining connection type <mdudley@king-cart.com>
    Re: Determining connection type <emschwar@fc.hp.com>
    Re: Determining connection type <1usa@llenroc.ude.invalid>
    Re: Determining connection type <No_4@dsl.pipex.com>
    Re: Determining connection type <noreply@gunnar.cc>
    Re: Dynamic Search Tool <1usa@llenroc.ude.invalid>
    Re: Generate Squential Numbers? <ahamm@mail.com>
    Re: Generate Squential Numbers? <1usa@llenroc.ude.invalid>
    Re: Generate Squential Numbers? <ahamm@mail.com>
    Re: Generate Squential Numbers? <ahamm@mail.com>
    Re: How to detect an undefined SV* value in XS? <jl_post@hotmail.com>
    Re: How to detect an undefined SV* value in XS? <jl_post@hotmail.com>
    Re: How to detect an undefined SV* value in XS? <jl_post@hotmail.com>
    Re: How to detect an undefined SV* value in XS? <kalinaubears@iinet.net.au>
    Re: How to detect an undefined SV* value in XS? <kalinaubears@iinet.net.au>
    Re: How to detect an undefined SV* value in XS? <kalinaubears@iinet.net.au>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 08 Dec 2004 04:31:04 +0000
From: Andrew Tkachenko <pobugfix@peterlink.ru>
Subject: better way of Event.pm usage
Message-Id: <cp5llb$65c$1@news.rol.ru>

Hello.

I'm trying to build a server aimed to be a 'bridge' between internal clients
and external servers (a kind of proxy server). So for each incoming
client's connection server creates another socket connected to external
server and then transfers data between those two sockets. Server allows up
to 300 simultaneously connected clients. 
I'm quite lazy so I'm trying to play with Event.pm willing to avoid
dispatching IO events by myself. So the question is: what is the better way
to use Event.pm :

a). create new pair of watchers each time when incoming connection detected
b). keep 600 watchers (300 for client socks and 300 for external socks) and 
modify watcher's 'fd' attribute (using $watcher->fd method) 'on-the-fly' ?


Thanks a lot for advance.
-- 
Andrew


------------------------------

Date: Tue, 07 Dec 2004 18:25:42 -0500
From: Marshall Dudley <mdudley@king-cart.com>
Subject: Determining connection type
Message-Id: <41B63BF6.8B31BAE9@king-cart.com>

In a script I have I have to throw an image normally if the connection
is normal, and securily if it is secure.  However I cannot find any way
in perl to tell if the connection is secure or non secure.  The $0
strips the http vs https off of the value.

Is there any way to determine this?

Thanks,

Marshall



------------------------------

Date: Tue, 07 Dec 2004 18:35:42 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Determining connection type
Message-Id: <GPadnSCpavDSoyvcRVn-jQ@adelphia.com>

(comp.lang.perl is dead. It's pining for the fjords, pushing up daisies, 
gone to the next world. It's an EX newsgroup. Followups trimmed.)

Marshall Dudley wrote:

> In a script I have I have to throw an image normally if the connection
> is normal, and securily if it is secure.  However I cannot find any way
> in perl to tell if the connection is secure or non secure.

Assuming you're using LWP to establish this connection - how could you 
*not* know? You gave it the URL to connect to, just look at it to see if 
it starts with "http://" or "https://".

 >  The $0
> strips the http vs https off of the value.

You're *very* confused. $0 is the name of the currently running script. 
It has nothing to do with network connections.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


------------------------------

Date: 7 Dec 2004 16:44:22 -0700
From: mfuhr@fuhr.org (Michael Fuhr)
Subject: Re: Determining connection type
Message-Id: <41b64056$1_1@omega.dimensional.com>

Marshall Dudley <mdudley@king-cart.com> writes:

> In a script I have I have to throw an image normally if the connection
> is normal, and securily if it is secure.

What do you mean by "throw an image", and how do you throw it
normally vs. securely?  Do you mean you're outputting a different
image depending on the connection type?  Or something else?

> However I cannot find any way in perl to tell if the connection is
> secure or non secure.

What connection are you talking about?  Is a web server invoking
your script via mod_perl, CGI, etc., and you want to know whether
the connection was made via HTTP or HTTPS?  If so, then depending
on how your script is invoked and what web server you're using, you
might be able to determine this by examining environment variables.
For example, $ENV{HTTPS} might be set for secure connections; run
some experiments to see how your system behaves.

> The $0 strips the http vs https off of the value.

What value are you talking about?  The value of $0?  What makes you
think "http" or "https" was ever part of that value in the first
place?

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/


------------------------------

Date: Tue, 07 Dec 2004 18:47:54 -0500
From: Marshall Dudley <mdudley@king-cart.com>
Subject: Re: Determining connection type
Message-Id: <41B6412A.20D5716@king-cart.com>

Sherm Pendley wrote:

> (comp.lang.perl is dead. It's pining for the fjords, pushing up daisies,
> gone to the next world. It's an EX newsgroup. Followups trimmed.)
>
> Marshall Dudley wrote:
>
> > In a script I have I have to throw an image normally if the connection
> > is normal, and securily if it is secure.  However I cannot find any way
> > in perl to tell if the connection is secure or non secure.
>
> Assuming you're using LWP to establish this connection - how could you
> *not* know? You gave it the URL to connect to, just look at it to see if
> it starts with "http://" or "https://".

Previous code run is indeterminate in a server environment.  The connection
is from a browser to the perl, not from the perl to a server.  I use LWP, but
not in this section.  The connection is coming in, not going out.

In most cases the url given it would be relative not absolute, so even if I
could see what was given it, it would not tell me anything unless I knew if
that connection was secure or not, and then I am back to the same problem

>
>  >  The $0
> > strips the http vs https off of the value.
>
> You're *very* confused. $0 is the name of the currently running script.
> It has nothing to do with network connections.

That is correct.  That is I want to see if the browser asked for
http://www.domain.com/cgi-bin/script.cgi or
https://www.domain.com/cgi-bin/script.cgi, the button the customer clicks on
may be either of the above or just plain old script.cgi or
/cgi-bin/script.cgi.  In any case the browser will put the http: or https: in
front upon the request regardless of whether it is an absolute or relative
url in the button depending on if that connection is secure or not.

Marshall



------------------------------

Date: Tue, 07 Dec 2004 18:56:44 -0500
From: Marshall Dudley <mdudley@king-cart.com>
Subject: Re: Determining connection type
Message-Id: <41B6433C.36F7D393@king-cart.com>

Michael Fuhr wrote:

> Marshall Dudley <mdudley@king-cart.com> writes:
>
> > In a script I have I have to throw an image normally if the connection
> > is normal, and securily if it is secure.
>
> What do you mean by "throw an image", and how do you throw it
> normally vs. securely?  Do you mean you're outputting a different
> image depending on the connection type?  Or something else?

This is a shopping cart application and the image is thrown from the images
directory.  Normally it is thrown non securely so that we don't have the
overhead of throwing it securily, but if we are on a secure page, it needs
to be thrown securily, that is the image tag needs to have https instead of
http in it.  I could go relative, which would work, but some stores actually
throw images from another site on the net, so that won't always work.

That is I need to use

<img src=https://domain.com/images/image.jpg> instead of <img
src=http://domain.com/images/image.jpg>  There are other issues as well,
such as if I need to close a secure form and reopen as an insecure form or
not, but it all depends on determining if the connection is an ssl
connection or not.  This is part of an error recovery routine (that the
shopping cart is empty and thus cannot be displayed, and the customer cannot
continue with checkout), so it can come from a dozen different pages, some
secure and some not secure.

>
>
> > However I cannot find any way in perl to tell if the connection is
> > secure or non secure.
>
> What connection are you talking about?  Is a web server invoking
> your script via mod_perl, CGI, etc., and you want to know whether
> the connection was made via HTTP or HTTPS?  If so, then depending
> on how your script is invoked and what web server you're using, you
> might be able to determine this by examining environment variables.
> For example, $ENV{HTTPS} might be set for secure connections; run
> some experiments to see how your system behaves.

Bingo!!!

OK that returns a null if not ssl, and 'on' if it is ssl. That is exactly
what I was looking for.

Many many thanks.

Marshall

>
>
> > The $0 strips the http vs https off of the value.
>
> What value are you talking about?  The value of $0?  What makes you
> think "http" or "https" was ever part of that value in the first
> place?
>
> --
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/



------------------------------

Date: Tue, 07 Dec 2004 16:58:33 -0700
From: Eric Schwartz <emschwar@fc.hp.com>
Subject: Re: Determining connection type
Message-Id: <etor7m1u0d2.fsf@wilson.emschwar>

Marshall Dudley <mdudley@king-cart.com> writes:
> Previous code run is indeterminate in a server environment.  The connection
> is from a browser to the perl, not from the perl to a server.

Assuming you're talking about a CGI program (and if you are, it's very
rude not to have mentioned this; I imagine you have at least 1/3 of
the population of this newsgroup wondering what the heck "throw an
image" means), then the connection is NOT from a browser to "the
perl", (if by "the perl" you mean "the perl program run via what I can
only assume is CGI").

The connection is from the browser to the webserver (via HTTP), and
the webserver then talks to your perl program (via CGI).

> I use LWP, but not in this section.  The connection is coming in,
> not going out.

This is pretty basic information we need to know to figure out what
your problem is.  Please give it first next time, instead of making us
ask for it, okay?

Also, you still haven't answered what you mean by "throw an image".
I'm going to guess that by "throw an image" you mean "serve an image
via CGI", so this answer may not be correct if that's not your
problem.  If it is your problem, then please state it clearly next
time, instead of making people guess.  That's rude-- it's implying
that it's everyone else's job to interpret your questions, instead of
it being your job to state them clearly in the first place.

If my guess is correct, the answer is that you don't *CARE* if the
connection is http or https.  That's the server's problem.  You just
use CGI to interact with the server, and let it worry about
negotiating a secure connection.

This is also NOT a Perl problem-- you can tell it's not a Perl
problem, because you'd have exactly the same difficulty if you were
using Ruby, or C, or, God forbid, COBOL, to write this CGI program.
Next time, if you have a question about CGI, just ask yourself, "Would
I have this same question if I were writing this program in
INTERCAL?".  If the answer is yes, then please direct your question to
comp.infosystems.www.authoring.cgi instead of comp.lang.perl.misc.

-=Eric
-- 
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
		-- Blair Houghton.


------------------------------

Date: 8 Dec 2004 00:32:54 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Determining connection type
Message-Id: <Xns95B8C6DBA4A6Fasu1cornelledu@132.236.56.8>

Marshall Dudley <mdudley@king-cart.com> wrote in
news:41B6433C.36F7D393@king-cart.com: 

> This is a shopping cart application and the image is thrown from the
> images directory.  

This is gibberish ... There is no accepted meaning of the phrase "throw an 
image". Show some respect to the people you are asking for help.

>> What connection are you talking about?  Is a web server invoking
>> your script via mod_perl, CGI, etc., and you want to know whether
>> the connection was made via HTTP or HTTPS?  If so, then depending
>> on how your script is invoked and what web server you're using, you
>> might be able to determine this by examining environment variables.

Note the operative word _might_ in the sentence above.

See http://cgi-spec.golux.com/

>> For example, $ENV{HTTPS} might be set for secure connections; run
>> some experiments to see how your system behaves.
> 
> Bingo!!!
> 
> OK that returns a null if not ssl, and 'on' if it is ssl. That is

So, this is a CGI script. In that case, why did you not read the CGI 
documentation before posting here? Why did you post a CGI question here 
anyway?

Please go ahead and read the posting guidelines for this group. They are 
posted here regularly and also available on the WWW:

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

Sinan


------------------------------

Date: Wed, 08 Dec 2004 00:44:17 +0000
From: Big and Blue <No_4@dsl.pipex.com>
Subject: Re: Determining connection type
Message-Id: <41b64e63$0$19154$cc9e4d1f@news-text.dial.pipex.com>

Marshall Dudley wrote:
>> 
> This is a shopping cart application and the image is thrown from the images
> directory.  Normally it is thrown non securely so that we don't have the
> overhead of throwing it securily, but if we are on a secure page, it needs
> to be thrown securily,

    It is customary to serve images, not throw them.  And, as has been 
mentioned, this is a CGI issue (in fact, it's an HTML one), not a Perl problem.

>        that is the image tag needs to have https instead of
> http in it.  I could go relative, which would work, but some stores actually
> throw images from another site on the net, so that won't always work.

    Well, use relative when they don't.

> That is I need to use
> 
> <img src=https://domain.com/images/image.jpg> instead of <img
> src=http://domain.com/images/image.jpg>  There are other issues as well,
> such as if I need to close a secure form and reopen as an insecure form or
> not, but it all depends on determining if the connection is an ssl
> connection or not.

    So read up on your Web server - how to configure it and use it.  It is 
that which is interacting with your program using CGI.

    But the answer you are probably looking for it to check your 
environment for SSL_* variables being set.

>>>However I cannot find any way in perl to tell if the connection is
>>>secure or non secure.

    That's because it isn't a Perl issue.  Perl may let you discover the 
answer, but you really shoudl get to grips with your Web server and how to 
run it first.  In particular, if you are writing a shopping-cart 
application (i.e., presumably some financial aspect to this) without such 
an understanding could you please publish the Website address here so that 
I may avoid using it?


-- 
      -*-    Just because I've written it here doesn't    -*-
      -*-    mean that you should, or I do, believe it.   -*-


------------------------------

Date: Wed, 08 Dec 2004 02:20:50 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Determining connection type
Message-Id: <31n36fF3e91g2U1@individual.net>

Marshall Dudley wrote:
> In a script I have I have to throw an image normally if the connection
> is normal, and securily if it is secure.  However I cannot find any way
> in perl to tell if the connection is secure or non secure.  The $0
> strips the http vs https off of the value.
> 
> Is there any way to determine this?

As others have said, check out the environment variables for a secure 
connection.

If you have similar questions in the future, 
comp.infosystems.www.authoring.cgi is a more appropriate newsgroup.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: 8 Dec 2004 00:35:51 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Dynamic Search Tool
Message-Id: <Xns95B8C75B7AA5Casu1cornelledu@132.236.56.8>

"xtracold" <newsgroups@xtracold.co.uk> wrote in 
news:1102447370.007509.127060@f14g2000cwb.googlegroups.com:

> Now, simply put, has anyone ever attempted this before? 

What have _you_ tried so far and what do you need help with?

Please read the posting guidelines for this group:

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

If you need someone to write something to your specifications, this is not 
the right place to ask. Instead, you might want to head over to 
http://jobs.perl.org/

Sinan


------------------------------

Date: Wed, 8 Dec 2004 11:09:50 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Generate Squential Numbers?
Message-Id: <31mv2gF3duoi3U1@individual.net>

Terry wrote:
>> Lots of possibilities here,
>>  i.e store value in separate file, rather like a hit counter script.
>
> Thanks for the response!  Can you point a few example scripts on how
> this is done?  I am new to programming and new to perl too.

You sound like you need the number to be persistent across different
executions of the script?

People commonly use modules like Data::Dumper (spelling and correct choice
not tested ;-) to export values, or just write it to a file and re-read
it. Most solutions are quite straight-forward and similar to any solution
you might make in any language.

I made a simple module that implements a persistent value in a little text
file. Efficiency is not great but it's workable for low volumes of
activity. I don't think the module is sufficiently interesting to submit
to CPAN, but here's the module file    Persist.pm:


use strict;
use warnings;

package Persist;

use Fcntl;
use Carp;


sub TIESCALAR {
    my $class = shift;
    my $filename = shift;

    my $self = { };

    local *FD;

    sysopen FD, $filename, O_RDWR | O_CREAT | O_SYNC
         or croak "cannot open $filename: $!";

    $self->{fd} = *FD{IO};
    my $value = <FD>;
    chomp $value if defined $value;
    $self->{value} = $value;

    bless $self, $class;
}   # TIESCALAR


sub STORE {
    my $self = shift;
    my $value = shift;

    $self->{value} = $value;

    local *FD = $self->{fd};
    seek FD, 0, 0;

    print FD "$value\n";
    truncate FD, tell FD;

    return $value;
}   # STORE


sub FETCH {
    return $_[0]->{value};
}   # FETCH

1;

__END__

This module (when I show you how to use it) will keep your variable stored
in a file, but it will not be 100% reliable if multiple instances of the
same script will use it in parallel. If your problem includes many scripts
running simultaneously you need an entirely different and more robust
solution. Do not trust my solution to keep your numbers unique in that
case!

You need to put this module file in a suitable place. If you maintain a
private library directory and know how to point to it, that's the go. Or
you can store it in the same directory as the script and access it in a
flexible way that doesn't demand hard-coded paths in your script. But
comfortably accessing this module is really another question with a few
possible answers that you might already know...

To use the module:

 ...... STUFF TO LOCATE THE MODULE ....
use Persist;
 ....
my $serial = 0;
my $filename = "integer.txt";
tie $serial, 'Persist', $filename;

Now you may treat $serial as an ordinary variable and whenever you assign
to it, it will be re-written to the file. Clearly not very efficient for
huge number of allocations in rapid succession.....

eg

    my $nextnum = ++$serial;

and away you go. When the script terminates the file will be closed
automatically perl.




------------------------------

Date: 8 Dec 2004 00:53:34 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Generate Squential Numbers?
Message-Id: <Xns95B8CA5C09167asu1cornelledu@132.236.56.8>

"Andrew Hamm" <ahamm@mail.com> wrote in
news:31mv2gF3duoi3U1@individual.net: 

> Terry wrote:
>>> Lots of possibilities here,
>>>  i.e store value in separate file, rather like a hit counter script.
>>
>> Thanks for the response!  Can you point a few example scripts on how
>> this is done?  I am new to programming and new to perl too.
> 
> You sound like you need the number to be persistent across different
> executions of the script?

Yes, indeed. That's why he would be best served by reading the FAQ (you 
might want to check that document as well).

> People commonly use modules like Data::Dumper (spelling and correct
> choice not tested ;-) to export values, or just write it to a file and
> re-read it. 

You need to make sure there is no chance more than one invocation of the 
script accessing the file at the same time.

> This module (when I show you how to use it) will keep your variable
> stored in a file, but it will not be 100% reliable if multiple
> instances of the same script will use it in parallel. If your problem
> includes many scripts running simultaneously you need an entirely
> different and more robust solution. Do not trust my solution to keep
> your numbers unique in that case!

Exactly, the OP is talking about a CGI script. 

I would have preferred the OP to try something on his own first. After 
all, that is more conducive to learning. But now that the cat is out of the 
bag, here is one way to do it. This is clearly different than the FAQ 
method in that I am not locking the file I am writing to but rather a file 
that just exists as a sentinel.

You will probably need to create the count and count.lock files from the 
command line with the appropriate permissions before the script will run 
successfully.

#! /usr/bin/perl

use strict;
use warnings;

use Fcntl qw':flock :seek';

my $count = update(
    lock_file  => '/path/to/count.lock',
    count_file => '/path/to/count',
);

print <<OUTPUT;
Content-type: text/html

<html>
<head>
<title>Count = $count</title>
</head>
<body>
<p>
The new count is: <big><strong>$count</strong></big>.
</p>
</body>
</html>
OUTPUT

sub update {
    my %args = @_;

    my $lock_file = $args{lock_file};
    my $count_file = $args{count_file};
    
    open my $LOCK_FILE, '<', $lock_file 
        or die "Cannot open $lock_file: $!";
    
    flock $LOCK_FILE, LOCK_EX 
        or die "Cannot lock $lock_file: $!";

    my $count;
    
    {
        open my $COUNT_FILE, '<', $count_file
            or die "Cannot open $count_file: $!";
        
        $count = <$COUNT_FILE>;
        {
            no warnings;
            $count += 1;
        }
        
        seek $COUNT_FILE, SEEK_SET, 0
            or die "Cannot seek to the start of $count_file: $!";
        
        print $COUNT_FILE "$count\n"
            or die "Cannot write new count to $count_file: $!";

        close $COUNT_FILE
            or die "Cannot close $count_file: $!";
    }

    close $LOCK_FILE
        or die "Cannot close $lock_file: $!";

    return $count;
}

__END__



------------------------------

Date: Wed, 8 Dec 2004 12:26:45 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Generate Squential Numbers?
Message-Id: <31n3inF3brdedU1@individual.net>

A. Sinan Unur wrote:
>
> Yes, indeed. That's why he would be best served by reading the FAQ
> (you might want to check that document as well).

Not really. Persistence in a number of situations and solutions is not
something i struggle with. I selected a basic answer because I thought
that Terry needed something simple to get started. A core part of my work
is dealing with massively multi-user OLTP systems so even the Perl
solutions are a bit tiddly-wink compared to a full transactional system.

> Exactly, the OP is talking about a CGI script.

Paying attention to that fact is important - point conceded

> I would have preferred the OP to try something on his own first. After
> all, that is more conducive to learning. But now that the cat is out
> of the bag

That's an interesting point of view that is common around here. For what
it's worth, I'm a local guru in my company for the programmers on a few
languages, tools and products, and I have found over the years that
concrete examples to follow generally leads to quicker understanding.
Thinking abstractly and nutting out a problem for the fun of it is one
thing, but especially in my company it's also important to get the
programmers moving forward achieving deadlines and understanding new ideas
through experience is also important.

Don't underestimate the value of hands-on experience either. I think
there's room for keeping the cat in the bag and letting it out.
Schroedinger would agree I'm sure. I will give a newsgroup poster (and a
co-worker) the benefit of the doubt if they don't appear to be lazy and
stupid and proud of it.

Hope you don't think I'm trying to lecture you. Just having a pleasant
conversation about the nature of learning and putting my viewpoint.




------------------------------

Date: Wed, 8 Dec 2004 12:28:53 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Generate Squential Numbers?
Message-Id: <31n3mnF3e471hU1@individual.net>

A. Sinan Unur wrote:
>
>> Use the time as a key so long as you are never likely to get 2 calls
>> in the same second.
>
> 'never likely' is not a meaningful statement. Something is either
> possible or not possible. If you cannot rule out the possibility, the
> fact that the probability might be small is not important.

Yes indeed. "Unlikely" is the death-knell of any
multi-user/multi-processing system. It leads to occasional conficts which
are some of the worst bugs to fix and the only evidence is a few corrupt
records amongst millions.




------------------------------

Date: 7 Dec 2004 15:08:28 -0800
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: How to detect an undefined SV* value in XS?
Message-Id: <1102460908.527611.238380@z14g2000cwz.googlegroups.com>

Ben Morrow said:
>
> I *believe* that all you need to do is
> if (sv != &PL_sv_undef)
>
> If I am wrong here I would appreciate correction... :)


I don't think that's correct.  I might be wrong, but I think that
&PL_sv_undef is a macro that's used when one wants to return an
undefined value, either by pushing it onto the return stack or setting
RETVAL to it, like this:

RETVAL = &PL_sv_undef;

Thanks to the replies of other posters, I now know of three
different ways to check for an undefined value:

if (SvTYPE(sv) == SVt_NULL)  printf("Undefined");
# Contributed by Rob (Sisyphus):
if (SvANY(sv) == NULL)  printf("Undefined");
# Contributed by Tassilo v. Parseval:
if (SvOK(sv) == false)  printf("Undefined");

Of these three methods, only SvOK is documented in "perldoc
perlapi", which is worth considering if portability is a concern.  But
I tested all three methods, and they all seem to work.
Thanks for all who replied!

   -- Jean-Luc



------------------------------

Date: 7 Dec 2004 15:09:31 -0800
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: How to detect an undefined SV* value in XS?
Message-Id: <1102460970.960811.244680@z14g2000cwz.googlegroups.com>

Ben Morrow said:
>
> I *believe* that all you need to do is
> if (sv != &PL_sv_undef)
>
> If I am wrong here I would appreciate correction... :)


I don't think that's correct.  I might be wrong, but I think that
&PL_sv_undef is a macro that's used when one wants to return an
undefined value, either by pushing it onto the return stack or setting
RETVAL to it, like this:

RETVAL = &PL_sv_undef;

Thanks to the replies of other posters, I now know of three
different ways to check for an undefined value:

if (SvTYPE(sv) == SVt_NULL)  printf("Undefined");
# Contributed by Rob (Sisyphus):
if (SvANY(sv) == NULL)  printf("Undefined");
# Contributed by Tassilo v. Parseval:
if (SvOK(sv) == false)  printf("Undefined");

Of these three methods, only SvOK is documented in "perldoc
perlapi", which is worth considering if portability is a concern.  But
I tested all three methods, and they all seem to work.
Thanks for all who replied!

   -- Jean-Luc



------------------------------

Date: 7 Dec 2004 15:11:56 -0800
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: How to detect an undefined SV* value in XS?
Message-Id: <1102461116.272149.18830@f14g2000cwb.googlegroups.com>

Ben Morrow said:
>
> I *believe* that all you need to do is
> if (sv != &PL_sv_undef)
>
> If I am wrong here I would appreciate correction... :)


I don't think that's correct.  I might be wrong, but I think that
&PL_sv_undef is a macro that's used when one wants to return an
undefined value, either by pushing it onto the return stack or setting
RETVAL to it, like this:

RETVAL = &PL_sv_undef;

Thanks to the replies of other posters, I now know of three
different ways to check for an undefined value:

if (SvTYPE(sv) == SVt_NULL)  printf("Undefined");
# Contributed by Rob (Sisyphus):
if (SvANY(sv) == NULL)  printf("Undefined");
# Contributed by Tassilo v. Parseval:
if (SvOK(sv) == false)  printf("Undefined");

Of these three methods, only SvOK is documented in "perldoc
perlapi", which is worth considering if portability is a concern.  But
I tested all three methods, and they all seem to work.
Thanks for all who replied!

   -- Jean-Luc



------------------------------

Date: Tue, 07 Dec 2004 22:09:37 +0000
From: Sisyphus <kalinaubears@iinet.net.au>
Subject: Re: How to detect an undefined SV* value in XS?
Message-Id: <41b63993$0$25760$5a62ac22@per-qv1-newsreader-01.iinet.net.au>

Ben Morrow wrote:
> Quoth jl_post@hotmail.com (J. Romano):
> 
>>Dear Perl community,
>>
>>   I have a problem here and I'm hoping that someone can help.
>>
>>   I am writing some XS code, and I need to detect whether a value
>>accessed through an SV pointer is defined or undefined.
> 
> 
> I *believe* that all you need to do is
> 
> SV *sv;
> 
> if (sv != &PL_sv_undef) {
>     ...
> }
> 
> . If I am wrong here I would appreciate correction... :)
> 

Even if sv *is* set to &PL_sv_undef, that seems to be an incorrect way 
to test for the condition. Simplest way to check on one's speculations 
is just to slap up an Inline C script:

use warnings;
use Inline C => Config =>
     BUILD_NOISY => 1; # to make sure we get to
                       # see compiler warnings

use Inline C => <<'END_OF_C_CODE';

void set_undef(SV * a) {

sv_setsv(a, &PL_sv_undef);

if(a == &PL_sv_undef)
   printf("A valid means of testing for &PL_sv_undef\n");

else printf("An INVALID means of testing for &PL_sv_undef\n");

}

END_OF_C_CODE

$x = 23;
set_undef($x);
if($x == undef) {print "OK\n"}

__END__

For me that produces:

An INVALID means of testing for &PL_sv_undef
Use of uninitialized value in numeric eq (==) at try.pl line 23.
Use of uninitialized value in numeric eq (==) at try.pl line 23.
OK

Not sure why the warning appears twice - however it's nice to be able to 
trick perl into spouting nonsense :-)

Cheers,
Rob

-- 
To reply by email u have to take out the u in kalinaubears.



------------------------------

Date: Tue, 07 Dec 2004 22:27:54 +0000
From: Sisyphus <kalinaubears@iinet.net.au>
Subject: Re: How to detect an undefined SV* value in XS?
Message-Id: <41b63ddc$0$25781$5a62ac22@per-qv1-newsreader-01.iinet.net.au>

jl_post@hotmail.com wrote:

> I looked around a little more and I was able to find what I was
> looking for.  It turns out that the "sv.h" header file defines a value
> called SVt_NULL that is returned by the SvTYPE() macro.  So I can do
> what I want like this:
> 
> if (SvTYPE(sv) == SVt_NULL)  printf("It's undef");
> else  printf("It aint undef");
> 
> And just like SvANY(), SVt_NULL isn't mentioned anywhere in "perldoc
> perlapi".  Go figure.
> 

At least the SvTYPE macro gets a mention .... but the perlapi 
documentation is notorious for being incomplete.

I think either macro will suit your purpose ('SvTYPE' is the more 
intuitive choice). As I mentioned in another post in this thread, 
simplest way to test things out is with an Inline C script.

Cheers,
Rob

-- 
To reply by email u have to take out the u in kalinaubears.



------------------------------

Date: Tue, 07 Dec 2004 23:08:44 +0000
From: Sisyphus <kalinaubears@iinet.net.au>
Subject: Re: How to detect an undefined SV* value in XS?
Message-Id: <41b6476d$0$25777$5a62ac22@per-qv1-newsreader-01.iinet.net.au>

Sisyphus wrote:

> 
> use warnings;
> use Inline C => Config =>
>     BUILD_NOISY => 1; # to make sure we get to
>                       # see compiler warnings
> 
> use Inline C => <<'END_OF_C_CODE';
> 
> void set_undef(SV * a) {
> 
> sv_setsv(a, &PL_sv_undef);
> 
> if(a == &PL_sv_undef)
>   printf("A valid means of testing for &PL_sv_undef\n");
> 
> else printf("An INVALID means of testing for &PL_sv_undef\n");
> 
> }
> 
> END_OF_C_CODE
> 
> $x = 23;
> set_undef($x);
> if($x == undef) {print "OK\n"}
> 
> __END__
> 
> For me that produces:
> 
> An INVALID means of testing for &PL_sv_undef
> Use of uninitialized value in numeric eq (==) at try.pl line 23.
> Use of uninitialized value in numeric eq (==) at try.pl line 23.
> OK
> 
> Not sure why the warning appears twice - however it's nice to be able to 
> trick perl into spouting nonsense :-)
> 
> Cheers,
> Rob
> 

Doh!! The warning appears twice because there are 2 "uninitialized 
values" ... and it's not nonsense ... and it would be more sensible to 
check that $x is !defined, than to check that $x == undef.

Sorry for that bit of noise. (Hope that's the full extent of it.)

Cheers,
Rob

-- 
To reply by email u have to take out the u in kalinaubears.



------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 7492
***************************************


home help back first fref pref prev next nref lref last post