Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Modules / September 2008



Tip: Looking for answers? Try searching our database.

IO::Prompt suggestion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sawyer X - 10 Sep 2008 10:52 GMT
I'm not sure if this is the place, so correct me if I'm wrong. I'm
just guessing this mailing list is more frequently read by those I
think are involved, so apologizes if this isn't the place..

I tried using Data::Validate::Domain (specifically exporting the
function is_domain) and obviously (to me, obviously, that is) using
the (relatively newly discovered for me) module IO::Prompt.

Thing is, it didn't work.
Here is a simple example:
while (!is_domain($site_name)) {
       # debugging
       print "site_name: $site_name\n";
       $site_name = prompt('Please enter a valid hostname: ');
       # debugging
       print "site_name: $site_name\n";
}

For some reason it always failed.
As you can see, I've added two lines to check what's wrong with the value.
I tried different things, till I decided to check it with Data::Dumper.
Low and behold, it returns an object. Here is the output:
site_name: $VAR1 = bless( {
                'success' => 1,
                'handled' => 1,
                'set_val' => 0,
                'value' => 'hello.com',
                'context' => 27
              }, 'IO::Prompt::ReturnVal' );

Obviously if I would remember TFM after RTFM I would know this by heart.

Still, maybe there should be a method, or parameter (IO::Prompt
already uses a lot of those) that indicates it shouldn't return an
object, but a simple string?
Right now I changed the code to:
while (!is_domain($site_name)) {
       $site_name = prompt('Please enter a valid hostname: ')->{value};
}
and it works perfectly.
David Precious - 10 Sep 2008 11:28 GMT
sawyer x wrote:
> As you can see, I've added two lines to check what's wrong with the value.
> I tried different things, till I decided to check it with Data::Dumper.
[quoted text clipped - 17 lines]
> }
> and it works perfectly.

The IO::Prompt::ReturnVal object should stringify to the value provided.

while (my $obj = IO::Prompt::prompt("say something: ")) {
    print "You said $obj\n";
}

The problem you're seeing is that Data::Validate::Domain assumes that,
if its first argument is a reference, it's being used OO-style, and
stores  it in $self.  It then has no domain to look at.

I think changing IO::Prompt to have an additional option is probably a
waste of time; after all, you won't know about the option unless you
read the docs, and if you read the docs, you'll know that you get back
an IO::Prompt::ReturnVal object, which stringifies as you'd expect :)

You could fix your code simply by changing is_domain($site_name) to
is_domain("$site_name") - although making it explicitly clear that
you're fetching the value from the IO::Prompt::ReturnVal object is
probably better coding, in that future programmers won't look at it,
wonder why the hell you bothered to double-quote a scalar, then find it
breaks when they remove the quotes.  (Nothing a suitable comment
wouldn't solve though).

Cheers

Dave P
Sawyer X - 10 Sep 2008 13:25 GMT
Thank you David for the reply and comment. I'll just explicitly comment what
I did.

> sawyer x wrote:
>
[quoted text clipped - 45 lines]
>
> Dave P
Bill Ward - 10 Sep 2008 19:41 GMT
> The IO::Prompt::ReturnVal object should stringify to the value provided.

Yes, it does, with a "use overload" argument:

   q{""} => sub { $_[0]{handled} = 1; "$_[0]{value}"; },

However, if you create a method as_string and change this to
\&as_string, then there's an easy way to do what the OP is asking for:
is_domain($site_name->as_string)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.