Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: get() and set() Easy methods #59

Open
FGasper opened this issue Feb 13, 2021 · 3 comments
Open

Feature Request: get() and set() Easy methods #59

FGasper opened this issue Feb 13, 2021 · 3 comments

Comments

@FGasper
Copy link
Contributor

FGasper commented Feb 13, 2021

It would be nice to be able to do:

$easy->set(
    URL => "http://example.com/",
    FILE => \$easy->{body},
);

… and $easy->get('CERTINFO').

This would obviate the need to import constants and in general make for a smoother experience of using Net::Curl.

@sparky, @eserte: I’m happy to implement this … thoughts?

@sparky
Copy link
Owner

sparky commented Feb 13, 2021

This is what Net::Curl::Simple was supposed to offer (and it does). Net::Curl uses constants, because speed. This way there is no need to convert back and forth between strings and integers.

@FGasper
Copy link
Contributor Author

FGasper commented Feb 14, 2021

This is what Net::Curl::Simple was supposed to offer (and it does).

N::C::S’s documentation says its interface is unstable; is that still true? I’ve never used it; I actually find Net::Curl’s separate multi and easy objects easier to conceptualize than N::C::S’s interface.

It’s definitely less efficient to use strings. I’m not proposing that setopt et al. go away; I’m just thinking something like the following for folks like myself who, for whatever reason, prefer Net::Curl versus N::C::S:

diff --git a/lib/Net/Curl/Easy.pm b/lib/Net/Curl/Easy.pm
index eaa45dc..94a6edb 100644
--- a/lib/Net/Curl/Easy.pm
+++ b/lib/Net/Curl/Easy.pm
@@ -10,6 +10,52 @@ our $VERSION = '0.48';
 our @EXPORT_OK = grep { /^CURL/x } keys %{Net::Curl::Easy::};
 our %EXPORT_TAGS = ( constants => \@EXPORT_OK );
 
+sub set {
+    splice @_, 1, 0, 'setopt';
+    &_set_or_push;
+}
+
+sub push {
+    splice @_, 1, 0, 'pushopt';
+    &_set_or_push;
+}
+
+sub _set_or_push {
+    my $self = shift;
+    my $method = shift;
+
+    my @to_set;
+    while (my ($k, $v) = splice @_, 0, 2) {
+        $k =~ tr<a-z><A-Z>;
+
+        $k = __PACKAGE__->can("CURLOPT_$k") || do {
+            require Carp;
+            Carp::croak("CURLOPT_$k doesn’t exist!");
+        };
+
+        $k = $k->();
+
+        CORE::push @to_set, [$k, $v];
+    }
+
+    $self->$method(@$_) for @to_set;
+
+    return $self;
+}
+
+sub get {
+    my ($self, $name) = @_;
+
+    $name =~ tr<a-z><A-Z>;
+
+    my $nameval = __PACKAGE__->can("CURLINFO_$name") || do {
+        require Carp;
+        Carp::croak("CURLINFO_$name doesn’t exist!");
+    };
+
+    return $self->getinfo($nameval);
+}
+
 ## no critic (ProhibitMultiplePackages)
 package Net::Curl::Easy::Code;

@FGasper
Copy link
Contributor Author

FGasper commented Feb 14, 2021

Net::Curl::Simple also seems to install good amount of not-necessarily-related stuff like POE. If it’s intended that that be the “quick-and-easy” interface to Net::Curl, would it make sense to lighten those dependencies?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants