-1

I asked a question here.

I want to make a subroutine for cleaning strings like this:

use strict;
use warnings;

sub cleanData($arg) {

    #remove A and B from string

    my $final_data = $arg;

    $final_data =~ s/A// ;
    $final_data =~ s/B/ /;

    print "final data: $final_data\n";

    return $final_data;

}

my $data = "11-A : 000-2B6888-00";
cleanData($data);

But I am getting these errors:

Illegal character in prototype for main::cleanData : $arg at Demo.pl line 4.

Global symbol "$arg" requires explicit package name at Demo.pl line 8.

Not enough arguments for main::cleanData at Demo.pl line 20, near "$data)"

Execution of Demo.pl aborted due to compilation errors.
Community
  • 1
  • 1
big_space
  • 71
  • 2
  • 10
  • 1
    You really *should* read a little about the `my` `our` syntax before asking a question about it. Perl's bundled documentation is excellent - you'll want to look at [`perlsub`](http://perldoc.perl.org/perlsub.html) for **Private Variables with my()**. [Coping with scoping](http://perl.plover.com/FAQs/Namespaces.html) provides a good summary and discussion. The error message is pretty good too. – G. Cito Nov 26 '14 at 21:04
  • http://perlmaven.com/subroutines-and-functions-in-perl – mpapec Nov 26 '14 at 21:05

2 Answers2

2

You want:

sub cleanData {
    my $final_data = shift;
...

With more arguments you might want to write:

sub cleanData {
    my ($final_data, $delete_re, $replace_re) = @_;

Of course there's nothing wrong with using a list assignment for a single element if it better suits your aesthetic.

sub cleanData {
    my ($final_data) = @_;

And if you are using a very modern version of Perl, with v5.20 and later, you could add:

no warnings "experimental::signatures";

to your current code and you'll be able to play with bleeding edge Perl.

tjd
  • 4,064
  • 1
  • 24
  • 34
  • cmd: perl -v. I am using version 5.x. But your method does not take an arg ! – big_space Nov 26 '14 at 20:53
  • 3
    @big_space Arguments in Perl are passed via `@_`. `shift` removes the 1st element (`$_[0]`) and copies it into `$final_data`. If you insist on making your code look like your favorite *other language* you can declare `sub cleanData ($) {`, but most experienced Perl programmers would not recommend that. Alternatively, with 5.20 you can play with the "signatures" feature.... – tjd Nov 26 '14 at 21:01
  • "bleeding edge Perl" means dangerous code ? – big_space Nov 26 '14 at 21:17
  • 3
    "experimental" means the syntax is likely to change. Using experimental features runs the risk of your code not being compatible with future versions of Perl. – tjd Nov 26 '14 at 21:21
-1

The problem is the syntax you are using to declare your function and access its arguments.

Take a look at perldoc perlsub: there are plenty of examples there of how to do both things.

Borodin
  • 126,100
  • 9
  • 70
  • 144
Daniel
  • 21,933
  • 14
  • 72
  • 101
  • 1
    @big_space: I am sure that your preferred language is much more "crappy" than Perl; I can level many accusations at it without even knowing what it is. If your belief in what you write is genuine then you have certainly chosen the wrong profession. – Borodin Nov 26 '14 at 21:23
  • 1
    @big_space I certainly wouldn't claim that a language that has been around for more than 20 years is crappy. It must be good for even some specific task isn't it ? – Daniel Nov 26 '14 at 21:34
  • @DWilches: I wouldn't attempt to reason with this guy. I am sure that he has very little programming experience and is being deliberately ignorant, although I can't imagine what motive he may have. – Borodin Nov 26 '14 at 21:44
  • @DWilches - Actually, I have no choice. I have to learn barely enough to make some simple 4-5 scripts. After that, I will never need to use if for another 1-2 years or more. If it was a main part of my work, I'd learn it in detail - textbook and all. Its so frustrating for someone who just wants to learn enough to make dirty scripts. Never happens with ruby. – big_space Nov 26 '14 at 23:32
  • @big_space: I am sure that your attitude is intentionally offensive, and I am surprised that you received any answers at all. I guess some people will do anything for reputation points. I hold classes in programming and, because of its exposed method calling mechanism, I have found that Perl is by far the best language to teach as a first language. I'm not clear what *"Never happens with ruby"* might mean, but Ruby's chaining syntax makes it very difficult for most beginners to understand unless they are familiar with using pipes to chain commands in Bash. Please try not to be so damned nasty. – Borodin Nov 27 '14 at 00:52
  • @Borodin - Offensive to who ? Its just a language that I don't like. I am surprised to see that perl is being used as a first language. Okay, I'll tone it down. Thanks. – big_space Nov 27 '14 at 01:08