Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:04:12+00:00 2026-05-27T18:04:12+00:00

I’m working on a dispatching script. It takes a string with a command, does

  • 0

I’m working on a dispatching script. It takes a string with a command, does some cooking to it, and then parses it. But I can’t grab a hold into the referencing:

Use::strict;
Use:warnings;

my($contexto, $cmd, $target, $ultpos, @params);
my $do = "echo5 sample string that says stuff ";

$target = "";
$cmd = "";
$_ = "";
# I do some cumbersome string parsing to get the array with
# the exploded string and then call parsear(@command)

sub parsear {

   my %operations = (
       'echo'   => \&echo,
       'status' => \&status,
       'echo5'  => \&echo5,
   );

   my $op = $_[0];
   if ($operations{$op}){
       $operations{$op}->(@_);
       print "it exists\n";
   }
   else{
       print "incorrect command.\n";
   }
}


sub status {
    print "correct status.\n";
}

sub echo {
    shift(@_);
    print join(' ',@_) . "\n";
}

sub echo5 {
    shift(@_);
    print join(' ',@_) . "\n" x 5;
}

I don’t really know what the problem is. If the sub does not exist, it never says "incorrect command", and if I call for example "echo5 hello" it should print out:

hello
hello
hello
hello
hello

But it does nothing.

And when I call echo, it works as expected. What is the explanation?

Note: I’m on the latest version of Strawberry Perl

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:04:13+00:00Added an answer on May 27, 2026 at 6:04 pm
    use strict;  # 'use' is a keyword
    use warnings;
    
    # All these variables are not needed
    
    sub parsear {  # Learn to indent correctly
        my %operations = (
            'echo'   => \&echo,
            'status' => \&status,
            'echo5'  => \&echo5,
        );
        my $op = shift;  # take first element off @_
        if ($operations{$op}) {
            print "$op exists\n";  # Make your status message useful
            $operations{$op}->(@_);
        } else {
            print "incorrect command: $op\n";  # And your error message
        }
    }
    
    sub status {
        print "correct status.\n";
    }
    
    sub echo {
        # shift(@_); # This is no longer needed, and now echo can be used as a
                     # normal subroutine as well as a dispatch target
        print join(' ',@_) . "\n";
    }
    
    sub echo5 {
        # shift(@_); # This is no longer needed
        print +(join(' ',@_) . "\n") x 5;  # Parentheses are needed since x binds tightly
    }
    

    Then running:

    parsear 'status';
    parsear 'echo', 'hello';
    parsear 'echo5', 'hello';
    parsear 'an error';
    

    results in:

    status exists
    correct status.
    echo exists
    hello
    echo5 exists
    hello
    hello
    hello
    hello
    hello
    incorrect command: an error
    

    I am not sure what "cumbersome string parsing" you are doing since you did not include it, but if you are parsing a string like

    my $do = "echo5 sample string that says stuff ";
    

    where the command is the first word, and the arguments are the rest, you can either split everything:

    parsear split /\s+/, $do;
    

    Or use a regex to cut the first word off:

    my ($cmd, $arg) = $do =~ /^(\w+)\s*(.*)/;
    
    parsear $cmd => $arg;
    

    You don’t even need the variables:

    parsear $do =~ /^(\w+)\s*(.*)/;
    

    Finally, the echo5 subroutine is a bit more complicated than it needs to be. It could be written as:

    sub echo5 {
        print "@_\n" x 5;  # "@_" means join($", @_) and $" defaults to ' '
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.